package com.sgg.datastructure.stack;

/**
 * 链表实现
 * 0.存储数据
 * 1.构造器
 * 2.满
 * 3.空
 * 4.push
 * 5.pop
 * 6.遍历
 */
public class LinkStackTwo {
    private StackNodeTwo top;

    public boolean isFull() {
        return false;
    }

    public boolean isEmpty() {
        return top == null;
    }

    public void push(int i) {
        if (top == null) {
            top = new StackNodeTwo(i);
            top.next = top;
            top.prev = top;
        } else {
            StackNodeTwo node = new StackNodeTwo(i);
            top.next = node;
            node.prev = top;
            top = node;
        }
    }

    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("空了");
        }
        int no = top.no;
        if (top != top.prev) {
            top = top.prev;
        } else {
            top = null;
        }
        return no;
    }

    public void show() {
        StackNodeTwo temp = top;
        while (true) {
            System.out.printf("遍历%d\n",temp.no);
            temp = temp.prev;
            if (temp.prev == temp) {
                System.out.println("遍历"+temp.no);
                break;
            }
        }
    }

    public static void main(String[] args) {
        LinkStackTwo linkStack = new LinkStackTwo();
        linkStack.push(1);
        linkStack.push(2);
        linkStack.push(3);
        linkStack.push(4);
        linkStack.show();
        System.out.println(linkStack.pop());
        System.out.println(linkStack.pop());
        System.out.println(linkStack.pop());
        System.out.println(linkStack.pop());
    }
}
